import numpy as np
import re
import copy


class Evaluation(object):
    def __init__(self):
        self.players=[1,2]
        self.width=15
        self.height=15
        self.judge_standard={
            'FIVE':1000000,#活五
            'FOUR':10000,#活四
            'THREE':1000,#活三
            'TWO':100,#活二
            'ONE':10,#活一
            'BLOCKFOUR':1000,#眠四
            'BLOCKTHREE':100,#眠三
            'BLOCKTWO':10,#眠二
            'BLOCKONE':1#眠一
        }
        self.black_table=self.compiletable(1)
        self.white_table=self.compiletable(2)
    def evaluate(self,board,player):
        black_v=0
        white_v=0
        moves,players=np.array(list(zip(*board.states.items())))
        matrix=[[0]*15 for _ in range(15)]
        move_black=[]
        move_white=[]
        move_black=moves[players==self.players[0]]
        move_white=moves[players==self.players[1]]
        for i in range(len(move_black)):
            matrix[move_black[i]//self.width][move_black[i]%self.width]=1
        for i in range(len(move_white)):
            matrix[move_white[i]//self.width][move_white[i]%self.width]=2
        matrix_white=copy.deepcopy(matrix)
        matrix_black=copy.deepcopy(matrix)
        for i in range(15):
            for j in range(15):
                if (matrix[i][j]==1):
                    black_v+=self.counter(self.direction(i,j,matrix_black),self.black_table)
                    matrix_black[i][j]='-'
                elif (matrix[i][j]==2):
                    white_v+=self.counter(self.direction(i,j,matrix_white),self.white_table)
                    matrix_white[i][j]='-'
                else:
                    continue
        if (player==self.players[0]):
            return (black_v-white_v)
        else:
            return (white_v-black_v)


    def direction(self,row,col,matrix):
        r=row
        c=col
        #按四个方向划分四个数组
        #水平方向
        horizon=[]
        for i in range(15):
            horizon.append(str(matrix[r][i]))
        #竖直方向
        vertical=[]
        for i in range(15):
            vertical.append(str(matrix[i][c]))
        #右上
        rightup=[]
        while(r<14 and c>0):
            r+=1
            c-=1
        while(r>=0 and c<=14):
            rightup.append(str(matrix[r][c]))
            r-=1
            c+=1
        #右下
        r=row
        c=col
        rightdown=[]
        while(r>0 and c>0):
            r-=1
            c-=1
        while(r<=14 and c<=14):
            rightdown.append(str(matrix[r][c]))
            r+=1
            c+=1

        horizoncheck='b'+''.join(horizon)+'b'
        verticalcheck='b'+''.join(vertical)+'b'
        rightupcheck='b'+''.join(rightup)+'b'
        rightdowncheck='b'+''.join(rightdown)+'b'
        return [horizoncheck,verticalcheck,rightupcheck,rightdowncheck]

    def compiletable(self,player):
        #创建对照表
        color=str(player)
        opplayer=(self.players[0] if player==self.players[1]
                                else self.players[1])
        ncolor=str(opplayer)
        table=[]
        #连五
        table.append(color+color+color+color+color)#0
        #活四
        table.append('0'+color+color+color+color+'0')#1
        #眠四
        table.append('b'+color+color+color+color+'0')#2
        table.append(ncolor+color+color+color+color+'0')#3
        table.append('0'+color+color+color+color+'b')#4
        table.append('0'+color+color+color+color+ncolor)#5
        table.append(color+color+'0'+color+color)#6
        table.append(color+'0'+color+color+color)#7
        table.append(color+color+color+'0'+color)#8
        #活三
        table.append('00'+color+color+color+'00')#9
        table.append('b0'+color+color+color+'00')#10
        table.append(ncolor+'0'+color+color+color+'00')#11
        table.append('00'+color+color+color+'0b')#12
        table.append('00'+color+color+color+'0'+ncolor)#13
        table.append('0'+color+'0'+color+color+'0')#14
        table.append('0'+color+color+'0'+color+'0')#15
        #眠三
        table.append('b0'+color+color+color+'0b')#16
        table.append(ncolor+'0'+color+color+color+'0'+ncolor)#17
        table.append(ncolor+'0'+color+color+color+'0b')#18
        table.append('b0'+color+color+color+'0'+ncolor)#19
        table.append(ncolor+color+color+color+'00')#20
        table.append('b0'+color+color+color+'00')#21
        table.append('00'+color+color+color+'0b')#22
        table.append('00'+color+color+color+'0'+ncolor)#23
        table.append('b'+color+color+'0'+color+'0')#24
        table.append(ncolor+color+color+'0'+color+'0')#25
        table.append('0'+color+'0'+color+color+ncolor)#26
        table.append('0'+color+'0'+color+color+'b')#27
        table.append(ncolor+color+'0'+color+color+'0')#28
        table.append('b'+color+'0'+color+color+'0')#29
        table.append('0'+color+color+'0'+color+ncolor)#30
        table.append('0'+color+color+'0'+color+'b')#31
        #活二
        table.append('00'+color+color+'00')#32
        table.append('0'+color+'0'+color+'0')#33
        table.append('0'+color+'00'+color+'0')#34
        #眠二
        table.append('b'+color+color+'000')#35
        table.append(ncolor+color+color+'000')#36
        table.append('000'+color+color+'b')#37
        table.append('000'+color+color+ncolor)#38
        table.append('b'+color+'0'+color+'00')#39
        table.append(ncolor+color+'0'+color+'00')#40
        table.append('00'+color+'0'+color+'b')#41
        table.append('00'+color+'0'+color+ncolor)#42
        table.append('b'+color+'00'+color+'0')#43
        table.append(ncolor+color+'00'+color+'0')#44
        table.append('0'+color+'00'+color+'b')#45
        table.append('0'+color+'00'+color+ncolor)#46
        #活一
        table.append('0'+color+'0')#47
        #眠一
        table.append('b'+color+'0')#48
        table.append(ncolor+color+'0')#49
        table.append('0'+color+'b')#50
        table.append('0'+color+ncolor)#51

        return table


    def counter(self,matrixlist,compilelist):
        #匹配当前位置周围的棋子排列和棋型
        countn=0
        size=0
        for i in range(4):
            for j in range(52):
                size=0 if ((compilelist[j] in matrixlist[i])==None) else matrixlist[i].count(compilelist[j])
                #已经匹配的字段用-代替，避免再次匹配
                matrixlist[i]=matrixlist[i].replace(compilelist[j],'-'*len(compilelist[j]))
                if (j==0):
                    countn+=size*self.judge_standard['FIVE']
                elif (j==1):
                    countn+=size*self.judge_standard['FOUR']
                elif (j>=2 and j<=8):
                    countn+=size*self.judge_standard['BLOCKFOUR']
                    if (j>=6):
                        countn+=size*self.judge_standard['FOUR']/5  
                elif (j>=9 and j<=15):
                    countn+=size*self.judge_standard['THREE']
                elif (j>=16 and j<=31):
                    countn+=size*self.judge_standard['BLOCKTHREE']
                elif (j>=32 and j<=34):
                    countn+=size*self.judge_standard['TWO']
                elif (j>=35 and j<=46):
                    countn+=size*self.judge_standard['BLOCKTWO']
                elif (j==47):
                    countn+=size*self.judge_standard['ONE']
                else:
                    countn+=size*self.judge_standard['BLOCKONE']
        return countn
